package com.pbdatabase.core.main;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.Properties;

public class PropertyManager {
	private static Logger _logger = Logger.getLogger(PropertyManager.class);
	private static PropertyManager _instance;

	public synchronized static PropertyManager getInstance() {
		if (null == _instance) {
			_instance = new PropertyManager();
		}
		return _instance;
	}

	private Properties properties;

	private PropertyManager() {
		properties = new Properties();
		try {
			//load a properties file
			properties.load(PropertyManager.class.getClassLoader().getResourceAsStream("config.properties"));

			//log status
			_logger.info("Loaded properties file");

			//log property value for debugging
			_logger.debug("Proto files location: " + properties.getProperty("pdb.proto.location"));
			_logger.debug("Generated files location: " + properties.getProperty("pdb.generated.location"));
			_logger.debug("Available nodes: " + properties.getProperty("pdb.nodes"));
		} catch (IOException ex) {
			_logger.error("Failed to load properties. Killing system.", ex);
			System.exit(-1);
		}
	}

	public String getProtoLocation() {
		return properties.getProperty("pdb.proto.location");
	}

	public String getMessageLocation() {
		return properties.getProperty("pdb.message.location");
	}

	public String getIndexLocation() {
		return properties.getProperty("pdb.index.location");
	}

	public String getGeneratedLocation() {
		return properties.getProperty("pdb.proto.location");
	}

	public String getThisNodeHash() {
		return properties.getProperty("pdb.this.node");
	}

	public String[] getNodes() {
		return properties.getProperty("pdb.nodes").split(",");
	}

}
